package tt.dz.service.imp.base;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tt.dz.dao.base.OmsCommonDao;
import tt.dz.dao.ext.charger.OmsChargerOfflineWhitelistDao;
import tt.dz.entity.base.JsonCommon;
import tt.dz.entity.base.JsonForList;
import tt.dz.entity.base.OmsCommonParam;
import tt.dz.entity.charger.OmsChargerOfflineWhitelist;
import tt.dz.entity.exception.OmsDaoException;
import tt.dz.service.base.OfflineWhiteListService;
import tt.dz.service.imp.middleware.SetServiceImpl;
import tt.dz.service.middleware.SetService;

@Service
public class OfflineWhitelistServiceImpl extends BaseService implements OfflineWhiteListService
{
	private Log log = LogFactory.getLog(OfflineWhitelistServiceImpl.class);

	@Autowired
	@Qualifier("base")
	private OmsCommonDao<OmsChargerOfflineWhitelist> omsCommonDaoImpl;

	@Autowired
	SetService setService;

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void addWhiteLists(String chargerId, List<String> list_userCard, String current_oper_id) throws Exception
	{
		try
		{
			Date now = new Date();
			for (String userCard : list_userCard)
			{
				OmsChargerOfflineWhitelist white = new OmsChargerOfflineWhitelist();
				white.setCharger_id(chargerId);
				white.setUser_pay_card(userCard);
				OmsChargerOfflineWhitelist record = omsCommonDaoImpl.findRecordByPK(white);
				if (record == null)
				{
					white.setValid_flag(1);
					white.setCreate_time(now);
					white.setCreate_oper_id(current_oper_id);
					white.setUpdate_time(now);
					white.setUpdate_oper_id(current_oper_id);
					omsCommonDaoImpl.save(white);
				}
				else
				{
					white.setValid_flag(1);
					white.setUpdate_time(now);
					white.setUpdate_oper_id(current_oper_id);
					omsCommonDaoImpl.update(white);
				}
			}

			setService.setWhiteList(chargerId, list_userCard, SetServiceImpl.OPER_ADD);
		}
		catch (Exception e)
		{
			log.error(e.getMessage(), e);
			throw e;
		}
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void deleteWhiteLists(String chargerId, List<String> list_userCard, String current_oper_id) throws Exception
	{
		try
		{
			Date now = new Date();
			List<String> deletedCards = new ArrayList<>();
			for (String userCard : list_userCard)
			{
				OmsChargerOfflineWhitelist white = new OmsChargerOfflineWhitelist();
				white.setCharger_id(chargerId);
				white.setUser_pay_card(userCard);
				OmsChargerOfflineWhitelist record = omsCommonDaoImpl.findRecordByPK(white);
				if (record != null)
				{
					white.setValid_flag(0);
					white.setUpdate_time(now);
					white.setUpdate_oper_id(current_oper_id);
					omsCommonDaoImpl.update(white);
					deletedCards.add(userCard);
				}
			}
			setService.setWhiteList(chargerId, deletedCards, SetServiceImpl.OPER_DELETE);
		}
		catch (Exception e)
		{
			log.error(e.getMessage(), e);
			throw e;
		}
	}

	@Override
	public JsonForList<?> findPageObjs(OmsCommonParam param, OmsChargerOfflineWhitelist offlineWhitelist)
	{
		try
		{
			List<OmsChargerOfflineWhitelist> lst = omsCommonDaoImpl.pagingFind(param, offlineWhitelist);
			int total = 0;
			if (param.isQueryTotal())
			{
				total = omsCommonDaoImpl.queryTotalByObject(offlineWhitelist);
			}
			return this.buildSuccessJsonForList(param, lst, total);
		}
		catch (Exception e)
		{
			this.logError(e.getMessage(), e);
			return this.buildError400JsonForList();
		}
	}

}
